/*
# ============================================================================
# 版权所有 Chilcoo.com，并保留所有权利。
# 网站地址: http://www.chilcoo.com/ccerp
# ----------------------------------------------------------------------------
# 这不是一个自由软件！你不能通过任何方式破解本软件.
# 使用:不允许对程序代码以任何形式任何目的的再发布。
# ============================================================================
# $Author: Chilcoo Inc. *
# $Official website  : www.chilcoo.com
# オーダー処理のページ
# 
# 開発日記：
# 2011－04－15
# ただいま購入用の基本データーの処理は一部完了しました。
# カートの処理と確認ビュー及びバックのAJAX処理はまだ完成していません。
*/


ajax_callback_page = '<{$ROOTPATH}>/order/default.php';

var order = {
	block:{
		cart:{id:'#cart_block'},
		pay:{
			id:'#pay_block', 
			cod:'#pay_block_cod', 
			cod_radio:'#pay_block_cod_radio',
			bank:'#pay_block_bank', 
			bank_radio:'#pay_block_bank_radio',
			change_view:'#pay_change_view',
			change_cancel_link : "#pay_change_cancel_link",
			pay_id:'#pay_id'
		},
		user_address:{
			id:'#user_address_block',
			onamae:'#user_address_block .onamae',
			furikana:'#user_address_block .furikana',
			company1:'#user_address_block .company1',
			company2:'#user_address_block .company2',
			zip:'#user_address_block .zip',
			ken:'#user_address_block .ken',
			city:'#user_address_block .city',
			address:'#user_address_block .address',
			mail:'#user_address_block .mail',
			tel:'#user_address_block .tel',
			mobile:'#user_address_block .mobile',
			addr_id:'#user_address_block .addr_id'
		},
		delivery_address:{
			id:'#delivery_address_block',
			onamae:'#delivery_address_block .onamae',
			zip:'#delivery_address_block .zip',
			ken:'#delivery_address_block .ken',
			city:'#delivery_address_block .city',
			address:'#delivery_address_block .address',
			tel:'#delivery_address_block .tel',
			mobile:'#delivery_address_block .mobile',
			same_user_link:'#delivery_address_same_user_link',
			same_user_cancel:'#delivery_address_same_user_cancel',
			same_user_cancel_link:'#delivery_address_same_user_cancel_link',
			addr_id:'#delivery_addr_id',
			same_view:'#delivery_address_same_view',
			form_view:'#delivery_address_form_view'
		},
		distribution:{
			id:'#distribution_block',
			select:'#delivery_date_select',
			time:'#delivery_time'
		},
		other:{
			id:'#other_block',
			need:'#need_invoice',
			cancel:'#cancel_invoice',
			invoice:'#invoice'
		},
		regist:{
			id:'#regist_block',
			r_id:'#r_id',
			r_pwd:'#r_pwd',
			r_pwd1:'#r_pwd1'
		},
		section:{
			id:'#section_block',
			r_sex1:'#r_sex1',
			r_sex2:'#r_sex2',
			r_age:'#r_age'
		},
		execute:{
			id:'execute_block',
			back:'#go_back',
			execute:'#go_execute'
		},
		error_msg:' .error_msg',
		error_msg_plus:' + .error_msg'
	},
	user:{id:'#user_id'},
	init:function(){
		// 处理：隐藏修改支付方式的模块
		$(this.block.pay.change_view).hide();
		// 处理：隐藏取消相同地址的模块
		$(this.block.delivery_address.same_user_cancel).hide();
		// 处理：隐藏发票输入框
		$(this.block.other.invoice).hide();
		// 处理：隐藏取消输入按钮
		$(this.block.other.cancel).hide();
		
		// 处理：点击货到付款方式
		$(this.block.pay.cod_radio).bind('click',function(){
			var obj = order.block.pay;
			$(obj.bank).fadeOut('slow',function(){
				$(obj.change_view).show();
				$(obj.pay_id).val(1);
				$(obj.id + obj.error_msg).html('*');
			});
		});
		// 处理：点击银行付款方式
		$(this.block.pay.bank_radio).bind('click',function(){
			var obj = order.block.pay;
			$(obj.cod).fadeOut('slow',function(){
				$(obj.change_view).show();
				$(obj.pay_id).val(2);
				$(obj.id + obj.error_msg).html('*');
			});
		});
		// 处理：取消付款方式，显示为重新选择
		$(this.block.pay.change_cancel_link).bind('click',function(){
			var obj = order.block.pay;
			$(obj.cod).show('slow');
			$(obj.bank).show('slow');
			$(obj.id + ' :radio').attr('checked','');
			$(obj.change_view).hide();
			$(obj.pay_id).val('');
		});	
		// 处理：发票
		$(this.block.other.need).bind('click',function(){
			var obj = order.block.other;
			$(obj.invoice).show();
			$(obj.cancel).show();
			$(obj.need).hide();
		});
		// 处理：发票
		$(this.block.other.cancel).bind('click',function(){
			var obj = order.block.other;
			$(obj.invoice).hide();
			$(obj.cancel).hide();
			$(obj.need).show();
		});
		// 处理：发货地址和登记地址相同的时候
		$(this.block.delivery_address.same_user_link).bind('click',function(){
			var obj = order.block.delivery_address;
			$(obj.addr_id).val(0);
			$(obj.form_view).hide('slow');
			$(obj.same_view).hide('slow');
			$(obj.same_user_cancel).show('slow');
		});
		// 处理：发货地址和登记地址不同的时候 取消
		$(this.block.delivery_address.same_user_cancel_link).bind('click',function(){
			var obj = order.block.delivery_address;
			$(obj.addr_id).val('new');
			$(obj.same_view).show('slow');
			$(obj.form_view).show('slow');
			$(obj.same_user_cancel).hide();
		});
		
		$(this.block.user_address.zip).bind('change', function(){
			var obj = order
			return obj.zip_addr($(this).val(),obj.block.user_address.id);
		});
		
		$(this.block.delivery_address.zip).bind('change', function(){
			var obj = order
			return obj.zip_addr($(this).val(),obj.block.delivery_address.id);
		});
		$(this.block.execute.execute).bind('click',function(){
			var obj = order
			if(order.validation_form()){
				order.execute_view();
			}
		});
		// 处理：发货指定日设置
		$(this.block.distribution.select).append
			('<option value="null"><{$order_js_distribution_select}></option>');
		var week = "<{$order_js_distribution_week}>";
		for(i=4;i<11;i++){
			var date = new Date( + new Date() + 24*60*60*1000*i);
			$(this.block.distribution.select).append(
				'<option value="' + date.getFullYear()+'<{$data_year}>'+
				date.getMonth()+'<{$data_month}>'+date.getDate()+'<{$data_day}>">'+
				date.getFullYear()+'<{$data_year}>'+date.getMonth()+'<{$data_month}>'+
				date.getDate()+'<{$data_day}>('+week.charAt(date.getDay())+')</option>');
			$(this.block.distribution.select).sSelect();
		}
	},
	execute_view:function(){
		var od = order_data;
		var o = order;
		// pay
		od.pay.pay_id = $(o.block.pay.pay_id).val();
		// user_address
		od.user_address.onamae = $(o.block.user_address.onamae).val();
		od.user_address.furikana = $(o.block.user_address.furikana).val();
		od.user_address.company1 = $(o.block.user_address.company1).val();
		od.user_address.company2 = $(o.block.user_address.company2).val();
		od.user_address.zip = $(o.block.user_address.zip).val();
		od.user_address.ken = $(o.block.user_address.ken).val();
		od.user_address.city = $(o.block.user_address.city).val();
		od.user_address.address = $(o.block.user_address.address).val();
		od.user_address.mail = $(o.block.user_address.mail).val();
		od.user_address.tel = $(o.block.user_address.tel).val();
		od.user_address.mobile = $(o.block.user_address.mobile).val();
		od.user_address.addr_id = $(o.block.user_address.addr_id).val();
		// delivery_address
		od.delivery_address.onamae = $(o.block.delivery_address.onamae).val();
		od.delivery_address.zip = $(o.block.delivery_address.zip).val();
		od.delivery_address.ken = $(o.block.delivery_address.ken).val();
		od.delivery_address.city = $(o.block.delivery_address.city).val();
		od.delivery_address.address = $(o.block.delivery_address.address).val();
		od.delivery_address.tel = $(o.block.delivery_address.tel).val();
		od.delivery_address.mobile = $(o.block.delivery_address.mobile).val();
		od.delivery_address.addr_id = $(o.block.delivery_address.addr_id).val();
		// distribution
		od.distribution.date = $(o.block.distribution.date).val();
		od.distribution.time = $(o.block.distribution.time).val();
		// other
		od.other.invoice = $(o.block.other.invoice).val();
		// regist
		od.regist.r_id = $(o.block.other.r_id).val();
		od.regist.r_pwd = $(o.block.other.r_pwd).val();
		od.regist.r_pwd1 = $(o.block.other.r_pwd1).val();
		// section
		od.section.r_sex1 = $(o.block.section.r_sex1).val();
		od.section.r_sex2 = $(o.block.section.r_sex2).val();
		od.section.r_age = $(o.block.section.r_age).val();
		
		// 这里不是调用ajax的地方,因为需要先在客户端进行数据确认后再在确认窗口里点击ajax调用
		$.ajax({
			url:ajax_callback_page,
			data:{token:TOKEN_ORDER_VIEW,data:od},
			success:function(data){
				alert(data.ref);
			},
			beforeSend:function(){
			}
		});
	},
	zip_addr:function(zipcode,block){
		if(zipcode.length < 7){
			$(block + ' .zip' + this.block.error_msg).html('* 郵便番号は正しくありません。');
			return;
		}else{
			$.ajax({
				url:AJAX_GLOBAL_CWS_JP_ZIP,
				data:{
					token:AJAX_GLOBAL_TOKEN_CWS_JP_ZIP,
					zip:zipcode
				},
				success:function(data){
					var setFocusText
					if(data.ken == null){
						$(block + ' .zip' + order.block.error_msg_plus).html('* 郵便番号は正しくありません。');
						setFocusText = $(block + ' .zip')
						return;
					}else{
						$(block + ' .zip' + order.block.error_msg_plus).html('');
						setFocusText = $(block + ' .address');
					}
					$(block + ' .ken').val(data.ken);
					$(block + ' .ken').sSelect();
					$(block + ' .city').val(data.city);
					$(block + ' .address').val(data.address);
					$(block + ' .zip').removeClass('textBlur');
					// 事件调用
					utilityHandle.setFocus(setFocusText,setFocusText.val().length);
					
					$.growlUI('HELP', '住所データーをロードしました。'); 
				},
				error:function(){
				},
				beforeSend:function(){
					$(block + ' .zip' + order.block.error_msg_plus).html('* ロード中です。');
				}
			});
		}
	},
	validation_form:function(){
		var checking = true;
		var scrolling = false;
		$(this.block.error_msg).html('*');
		// pay模块验证
		if($(this.block.pay.pay_id).val() == ''){
			$(this.block.pay.id + this.block.error_msg).html('* 必ず選択してください。');
			if(!scrolling){
				scrollTo(this.block.pay.id);
				scrolling = true;
				$(this.block.pay.id).focus();
			}
			checking = false;
		}
		// user_address模块验证
		if(utilityHandle.trim($(this.block.user_address.onamae).val()).length == 0){
			$(this.block.user_address.onamae + this.block.error_msg_plus).html('* 必ず記入してください。');
			if(!scrolling){
				scrollTo(this.block.user_address.onamae);
				scrolling = true;
				$(this.block.user_address.onamae).focus();
			}
			checking = false;
		}
		
		if(utilityHandle.trim($(this.block.user_address.furikana).val()).length == 0){
			$(this.block.user_address.furikana + this.block.error_msg_plus).html('* 必ず記入してください。');
			if(!scrolling){
				scrollTo(this.block.user_address.furikana);
				scrolling = true;
				$(this.block.user_address.furikana).focus();
			}
			checking = false;
		}
		
		if(utilityHandle.trim($(this.block.user_address.zip).val()).length == 0){
			$(this.block.user_address.zip + this.block.error_msg_plus).html('* 必ず記入してください。');
			if(!scrolling){
				scrollTo(this.block.user_address.zip);
				scrolling = true;
				$(this.block.user_address.zip).focus();
			}
			checking = false;
		}
		if(utilityHandle.trim($(this.block.user_address.mail).val()).length == 0){
			$(this.block.user_address.mail + this.block.error_msg_plus).html('* 必ず記入してください。');
			if(!scrolling){
				scrollTo(this.block.user_address.mail);
				scrolling = true;
				$(this.block.user_address.mail).focus();
			}
			checking = false;
		}
		if(!validation.isEmail($(this.block.user_address.mail).val())){
			$(this.block.user_address.mail + this.block.error_msg_plus).html('* メールアドレスは正しくありません。');
			if(!scrolling){
				scrollTo(this.block.user_address.mail);
				scrolling = true;
				$(this.block.user_address.mail).focus();
			}
			checking = false;
		}
		
		if(utilityHandle.trim($(this.block.user_address.tel).val()).length == 0
			 && utilityHandle.trim($(this.block.user_address.mobile).val()).length == 0){
			$(this.block.user_address.tel + this.block.error_msg_plus).html('* 必ず記入してください。');
			$(this.block.user_address.mobile + this.block.error_msg_plus).html('* 必ず記入してください。');
			if(!scrolling){
				scrollTo(this.block.user_address.tel);
				scrolling = true;
				$(this.block.user_address.tel).focus();
			}
			checking = false;
		}else if(utilityHandle.trim($(this.block.user_address.tel).val()).length != 0
			 && utilityHandle.trim($(this.block.user_address.mobile).val()).length == 0){
			if(utilityHandle.trim($(this.block.user_address.tel).val()).length < 10){
				$(this.block.user_address.tel + this.block.error_msg_plus).html('* 電話番号を正しく記入してください。1');
				if(!scrolling){
					scrollTo(this.block.user_address.tel);
					scrolling = true;
					$(this.block.user_address.tel).focus();
				}
				checking = false;
			}else if(!validation.isNumber($(this.block.user_address.tel).val())){
				$(this.block.user_address.tel + this.block.error_msg_plus).html('* 電話番号を正しく記入してください。2');
				if(!scrolling){
					scrollTo(this.block.user_address.tel);
					scrolling = true;
					$(this.block.user_address.tel).focus();
				}
				checking = false;
			}
		}else if(utilityHandle.trim($(this.block.user_address.tel).val()).length == 0
			 && utilityHandle.trim($(this.block.user_address.mobile).val()).length != 0){
			if(utilityHandle.trim($(this.block.user_address.mobile).val()).length < 10){
				$(this.block.user_address.mobile + this.block.error_msg_plus).html('* 電話番号を正しく記入してください。1');
				if(!scrolling){
					scrollTo(this.block.user_address.mobile);
					scrolling = true;
					$(this.block.user_address.mobile).focus();
				}
				checking = false;
			}else if(!validation.isNumber($(this.block.user_address.mobile).val())){
				$(this.block.user_address.mobile + this.block.error_msg_plus).html('* 電話番号を正しく記入してください。2');
				if(!scrolling){
					scrollTo(this.block.user_address.mobile);
					scrolling = true;
					$(this.block.user_address.mobile).focus();
				}
				checking = false;
			}
		}
		// delivery_address模块验证
		if($(this.block.delivery_address.addr_id).val() == 'new'){
			// 需要验证发货地址信息
			if(utilityHandle.trim($(this.block.delivery_address.onamae).val()).length == 0){
				$(this.block.delivery_address.onamae + this.block.error_msg_plus).html('* 必ず記入してください。');
				if(!scrolling){
					scrollTo(this.block.delivery_address.onamae);
					scrolling = true;
					$(this.block.delivery_address.onamae).focus();
				}
				checking = false;
			}
			if(utilityHandle.trim($(this.block.delivery_address.zip).val()).length == 0){
				$(this.block.delivery_address.zip + this.block.error_msg_plus).html('* 必ず記入してください。');
				if(!scrolling){
					scrollTo(this.block.delivery_address.zip);
					scrolling = true;
					$(this.block.delivery_address.zip).focus();
				}
				checking = false;
			}
			if(utilityHandle.trim($(this.block.delivery_address.tel).val()).length == 0
				&& utilityHandle.trim($(this.block.delivery_address.mobile).val()).length == 0){
				$(this.block.delivery_address.tel + this.block.error_msg_plus).html('* 必ず記入してください。');
				$(this.block.delivery_address.mobile + this.block.error_msg_plus).html('* 必ず記入してください。');
				if(!scrolling){
					scrollTo(this.block.delivery_address.tel);
					scrolling = true;
					$(this.block.delivery_address.tel).focus();
				}
				checking = false;
			}else if(utilityHandle.trim($(this.block.delivery_address.tel).val()).length != 0
				&& utilityHandle.trim($(this.block.delivery_address.mobile).val()).length == 0){
				if(utilityHandle.trim($(this.block.delivery_address.tel).val()).length < 10){
					$(this.block.delivery_address.tel + this.block.error_msg_plus).html('* 電話番号を正しく記入してください。1');
					if(!scrolling){
						scrollTo(this.block.delivery_address.tel);
						scrolling = true;
						$(this.block.delivery_address.tel).focus();
					}
					checking = false;
				}else if(!validation.isNumber($(this.block.delivery_address.tel).val())){
					$(this.block.delivery_address.tel + this.block.error_msg_plus).html('* 電話番号を正しく記入してください。2');
					if(!scrolling){
						scrollTo(this.block.delivery_address.tel);
						scrolling = true;
						$(this.block.delivery_address.tel).focus();
					}
					checking = false;
				}
			}else if(utilityHandle.trim($(this.block.delivery_address.tel).val()).length == 0
				 && utilityHandle.trim($(this.block.delivery_address.mobile).val()).length != 0){
				if(utilityHandle.trim($(this.block.delivery_address.mobile).val()).length < 10){
					$(this.block.delivery_address.mobile + this.block.error_msg_plus).html('* 電話番号を正しく記入してください。1');
					if(!scrolling){
						scrollTo(this.block.delivery_address.mobile);
						scrolling = true;
						$(this.block.delivery_address.mobile).focus();
					}
					checking = false;
				}else if(!validation.isNumber($(this.block.delivery_address.mobile).val())){
					$(this.block.delivery_address.mobile + this.block.error_msg_plus).html('* 電話番号を正しく記入してください。2');
					if(!scrolling){
						scrollTo(this.block.delivery_address.mobile);
						scrolling = true;
						$(this.block.delivery_address.mobile).focus();
					}
					checking = false;
				}
			}
		}
		/*
		//
		// 发货地址与进货地址相同,
		// 不需要验证发货地址信息
		// 
		else if($(this.block.delivery_address.addr_id).val() == 0){}
		*/
		// regist 模块验证
		if(utilityHandle.trim($(this.block.regist.r_id).val()).length == 0
			&& utilityHandle.trim($(this.block.regist.r_pwd).val()).length == 0
			&& utilityHandle.trim($(this.block.regist.r_pwd1).val()).length == 0){
			// 用户没有注册操作
		}else{
			//验证用户注册信息
			if(utilityHandle.trim($(this.block.regist.r_id).val()).length == 0){
				$(this.block.regist.r_id + this.block.error_msg_plus).html('* 必ず記入してください。');
				if(!scrolling){
					scrollTo(r_id);
					scrolling = true;
					$(r_id).focus();
				}
				checking = false;
			}
			if(utilityHandle.trim($(this.block.regist.r_pwd).val()).length == 0){
				$(this.block.regist.r_pwd + this.block.error_msg_plus).html('* 必ず記入してください。');
				if(!scrolling){
					scrollTo(this.block.regist.r_pwd);
					scrolling = true;
					$(this.block.regist.r_pwd).focus();
				}
				checking = false;
			}
			if(utilityHandle.trim($(this.block.regist.r_pwd1).val()).length == 0){
				$(this.block.regist.r_pwd1 + this.block.error_msg_plus).html('* 必ず記入してください。');
				if(!scrolling){
					scrollTo(this.block.regist.r_pwd1);
					scrolling = true;
					$(this.block.regist.r_pwd1).focus();
				}
				checking = false;
			}
			if(utilityHandle.trim($(this.block.regist.r_pwd).val()).length < 6){
				$(this.block.regist.r_pwd + this.block.error_msg_plus).html('* パスワード必ず6文字以上');
				if(!scrolling){
					scrollTo(this.block.regist.r_pwd);
					scrolling = true;
					$(this.block.regist.r_pwd).focus();
				}
				checking = false;
			}
			if(utilityHandle.trim($(this.block.regist.r_pwd1).val()).length  < 6){
				$(this.block.regist.r_pwd1 + this.block.error_msg_plus).html('* パスワード必ず6文字以上');
				if(!scrolling){
					scrollTo(this.block.regist.r_pwd1);
					scrolling = true;
					$(this.block.regist.r_pwd1).focus();
				}
				checking = false;
			}
			if($(this.block.regist.r_pwd).val() != $(this.block.regist.r_pwd1).val()){
				$(this.block.regist.r_pwd + this.block.error_msg_plus).html('* パスワードと再確認は異なります。');
				$(this.block.regist.r_pwd1 + this.block.error_msg_plus).html($(this.block.regist.r_pwd + this.block.error_msg_plus).html());
				if(!scrolling){
					scrollTo(this.block.regist.r_pwd);
					scrolling = true;
					$(this.block.regist.r_pwd).focus();
				}
				checking = false;
			}
		}
		return checking;
	}
}

// order data sets
var order_data = {
	cart:{},
	pay:{pay_id:null},
	user_address:{onamae:null,furikana:null,company1:null,company2:null,
		zip:null,ken:null,city:null,address:null,mail:null,tel:null,mobile:null,addr_id:null},
	delivery_address:{onamae:null,zip:null,ken:null,city:null,address:null,tel:null,mobile:null,addr_id:null},
	distribution:{date:null,time:null},
	other:{invoice:null},
	regist:{r_id:null,r_pwd:null,r_pwd1:null},
	section:{r_sex1:null,r_sex2:null,r_age:null}
}